﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using Report.Helper;
using Report.Entity;
using Report.Bussiness;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Report.Controllers
{
    /// <summary>
    /// 人员
    /// </summary>
    [Route("api/[controller]")]
    public class UserController : Controller
    {
        private UserBLL BLL = new UserBLL();
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="para">{"username":"用户名/电话","psd":"密码"}</param>
        /// <returns></returns>

        [HttpPost]
        [Route("Login")]
        public JsonResult Login([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            //查询用户信息
            string username = para["username"].ToString();
            string psd = para["psd"].ToString();
            sys_user entity = BLL.Login(username, psd);
            if (entity == null || entity.DEL)
            {
                var jserr = new { recode = 0, msg = "登录失败" };
                return Json(jserr);
            }
            //产生token
            TokenModel tm = new TokenModel()
            {
                Uid = entity.UserName,
                Role = "Admin",
                Dept = entity.UserID,
                TokenType = "Web"
            };
            string token = "Bearer " + JwtHelper.IssueJWT(tm);
            var jsok = new
            {
                recode = 1,
                info = new
                {
                    userid = entity.UserID,
                    username = entity.UserName,
                    token = token
                }
            };

            return Json(jsok);
        }
    }
}
